package com.quick.develop.form.controller;

import com.quick.develop.framework.common.page.PageResult;
import com.quick.develop.framework.common.utils.Result;
import com.quick.develop.form.convert.GenDynamicFormConvert;
import com.quick.develop.form.entity.GenDynamicFormEntity;
import com.quick.develop.form.query.GenDynamicFormQuery;
import com.quick.develop.form.vo.FormConfigVO;
import com.quick.develop.form.vo.GenDynamicFormVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import com.quick.develop.form.service.GenDynamicFormService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 动态表单
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2023-09-24
 */
@RestController
@RequestMapping("maku-generator/gen_dynamic_form")
@Tag(name = "动态表单")
@AllArgsConstructor
public class GenDynamicFormController {


    private final GenDynamicFormService genDynamicFormService;

    @GetMapping("page")
    @Operation(summary = "分页")
    public Result<PageResult<GenDynamicFormVO>> page(@Valid GenDynamicFormQuery query) {
        PageResult<GenDynamicFormVO> page = genDynamicFormService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    public Result<GenDynamicFormVO> get(@PathVariable("id") Long id) {
        GenDynamicFormEntity entity = genDynamicFormService.getById(id);

        return Result.ok(GenDynamicFormConvert.INSTANCE.convertVO(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    public Result<String> save(@RequestBody GenDynamicFormVO vo) {
        genDynamicFormService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    public Result<String> update(@RequestBody @Valid GenDynamicFormVO vo) {
        genDynamicFormService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    public Result<String> delete(@RequestBody List<Long> idList) {
        genDynamicFormService.delete(idList);

        return Result.ok();
    }

    @GetMapping("get-dynamic-form")
    public Result<FormConfigVO> getDynamicForm(FormConfigVO formConfigVO) {
        return Result.ok(genDynamicFormService.getDynamicForm(formConfigVO));
    }

    @GetMapping("get-dynamic-form-key")
    public Result<FormConfigVO> getDynamicFormByFormKey(FormConfigVO formConfigVO) {
        return Result.ok(genDynamicFormService.getDynamicForm(formConfigVO));
    }

    @PostMapping(value = "cv/{formId}")
    public Result<?> formCV(@PathVariable(value = "formId") Integer formId) {
        genDynamicFormService.formCV(formId);
        return Result.ok();
    }
}